add element in the last in double linked list in java
60
add element in the last in double linked list in java -
void push_back(int newElement) {
//1. allocate node
Node newNode = new Node();
//2. assign data element
newNode.data = newElement;
//3. assign null to the next and prev
// of the new node
newNode.next = null;
newNode.prev = null;
//4. Check the list is empty or not,
// if empty make the new node as head
if(head == null) {
head = newNode;
} else {
//5. Else, traverse to the last node
Node temp = new Node();
temp = head;
while(temp.next != null)
temp = temp.next;
//6. Adjust the links
temp.next = newNode;
newNode.prev = temp;
}
}